package com.dp.appi.dao.impl;

import com.dp.appi.dao.BaseDao;
import com.dp.appi.dao.CheckStatisticsDao;
import org.springframework.data.annotation.Persistent;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/6/8.
 */
//@Repository
@Component
public class CheckStatisticsDaoImpl extends BaseDaoImpl implements CheckStatisticsDao {
    /**
     * 返回没有设置上联的点位（除搭火点）
     *
     * @param param
     * @return
     */
    @Override
    public List<Map<String, Object>> joinCon(Map<String, String> param) {
        Object classify = param.get("classify");
        Object state_id = param.get("stage_id");
        Object type = param.get("type");
        return getJdbcTemplate().queryForList(this.joinCon_sql(), new Object[]{classify,type, state_id});
    }


    /**
     * 返回没有配置典设的点位
     *
     * @param param
     * @return
     */
    @Override
    public List<Map<String, Object>> codeset(Map<String, String> param) {
        Object classify = param.get("classify");
        Object state_id = param.get("stage_id");
        Object type = param.get("type");
        return getJdbcTemplate().queryForList(this.codeset(), new Object[]{classify,type, state_id});
    }


    /**
     * 返回供电半径不满足条件的点位
     *
     * @param param
     * @return
     */
    @Override
    public List<Map<String, Object>> supplyRadius(Map<String, String> param) {
        Object classify = param.get("classify");
        Object state_id = param.get("stage_id");
        Object type = param.get("type");
        List<Map<String, Object>> returnList = new ArrayList();
        //获得是变压器和380的电杆的集合
        StringBuilder sbb = new StringBuilder();
        sbb.append(" select * from p_exploration pex");
        sbb.append(" INNER JOIN p_pt pt ON pt.pt_id = pex.pt_id AND pt.pt_type = 11");
        sbb.append(" inner JOIN p_stage ps on pex.stage_id = ps.stage_id");
        sbb.append(" inner join p_baseinfo pb on pb.project_id = ps.project_id");
        sbb.append(" where pex.deleted = 0");
        sbb.append(" and pex.classify =?");
        sbb.append(" and pex.pt_id in (100004,100002)");
        sbb.append(" and pex.exploration_type = ?");
        sbb.append(" and pex.stage_id = ?");
        List<Map<String, Object>> list = findForJdbc(sbb.toString(), new Object[]{classify,type, state_id});

        if (list.size() > 0) {
            Integer power_zone = new Integer(list.get(0).get("power_zone").toString());//供电分区
            for (int i = 0; i < list.size(); i++) {
                Long exploration_id = Long.valueOf(list.get(i).get("exploration_id").toString());//踏勘点主键id
                Integer voltage_grade = new Integer(list.get(i).get("voltage_grade").toString());//点位电压等级
                Long pt_id = Long.valueOf(list.get(i).get("pt_id").toString());//点类型标识 变压器、搭火点.......
                Float allDistance = new Float(0);
                Float fd = new Float(0);
                if (voltage_grade == 101) {
                    if (power_zone == 11)
                        fd = Float.valueOf(3000);
                    else if (power_zone == 12)
                        fd = Float.valueOf(3000);
                    else if (power_zone == 13)
                        fd = Float.valueOf(3000);
                    else if (power_zone == 14)
                        fd = Float.valueOf(5000);
                    else if (power_zone == 15)
                        fd = Float.valueOf(15000);
                } else if (voltage_grade == 102) {
                    if (power_zone == 11)
                        fd = Float.valueOf(150);
                    else if (power_zone == 12)
                        fd = Float.valueOf(150);
                    else if (power_zone == 13)
                        fd = Float.valueOf(250);
                    else if (power_zone == 14)
                        fd = Float.valueOf(400);
                    else if (power_zone == 15)
                        fd = Float.valueOf(500);
                }
                if (pt_id == 100002) {
                    allDistance = getAllDistance_BYQ(exploration_id, new Long(100001));
                } else if (pt_id == 100004 && voltage_grade == 102) {
                    allDistance = getAllDistance_BYQ(exploration_id, new Long(100002));
                }

                if (allDistance > 0 && allDistance > fd) {
                    returnList.add(list.get(i));
                }
            }
        }
        return returnList;
    }

    /**
     * 返回档距不满足要求的点位
     *
     * @param param
     * @return
     */
    @Override
    public List<Map<String, Object>> span(Map<String, String> param) {
        Object classify = param.get("classify");
        Object state_id = param.get("stage_id");
        Object type = param.get("type");
        List<Map<String, Object>> list = getJdbcTemplate().queryForList(this.span(), new Object[]{classify,type, state_id});

        List<Map<String, Object>> returnList = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            String wire_type = list.get(i).get("wire_type").toString();
            String voltage_grade = list.get(i).get("voltage_grade").toString();
            Float distance = Float.valueOf(list.get(i).get("voltage_grade").toString());

            if (wire_type.equals("13") && voltage_grade.equals("101")) {
                if ((distance - 120) > 0)
                    returnList.add(list.get(i));
            } else {
                if ((distance - 80) > 0)
                    returnList.add(list.get(i));
            }
        }
        return returnList;
    }

    //供电距离合计
    @Override
    public Float getAllDistance_BYQ(Long exploration_id, Long pt_id) {
        Float AllDistance = new Float(0);
        String sql = "select * from p_exploration pex where pex.exploration_id = ?";
        Map<String, Object> map = findOneForJdbc(sql, new Object[]{exploration_id});
        Object before_exp = map.get("before_exp");//上级级联对象
        boolean flag = false;

        while (before_exp != null) {
            AllDistance += Float.valueOf(map.get("distance").toString());
            map = findOneForJdbc(sql, new Object[]{Long.valueOf(before_exp.toString())});
            before_exp = map.get("before_exp");//上级级联对象
            Long ptId = Long.valueOf(map.get("pt_id").toString());
            if (ptId == pt_id) {
                flag = true;
                break;
            }
        }

        if (!flag) {
            AllDistance = new Float(0);
        }
        return AllDistance;
    }


    /**
     * 返回测算档距是否合适的点位集合sql
     *
     * @return
     */
    private String span() {
        StringBuilder sb = new StringBuilder();
        sb.append(" select pex.exploration_id,pex.number,pt.name,pb.wire_type,pex.voltage_grade from p_exploration pex");
        sb.append(" INNER JOIN p_pt pt ON pt.pt_id = pex.pt_id AND pt.pt_type = 11");
        sb.append(" inner JOIN p_stage ps on pex.stage_id = ps.stage_id");
        sb.append(" inner join p_baseinfo pb on pb.project_id = ps.project_id");
        sb.append(" where pex.deleted = 0");
        sb.append(" and pex.classify = ?");
        sb.append("  and pex.distance is not null");
        sb.append(" and pb.wire_type is not NULL");
        sb.append(" and pex.voltage_grade is not null");
        sb.append(" and pex.exploration_type = ?");
        sb.append(" and pex.stage_id = ?");

        return sb.toString();
    }

    /**
     * 返回为设置上联的点位（除搭火点）
     *
     * @return
     */
    private String joinCon_sql() {
        StringBuilder sb = new StringBuilder();
        sb.append(" select * from p_exploration pex");
        sb.append(" INNER JOIN p_pt pt ON pt.pt_id = pex.pt_id AND pt.pt_type = 11");
        sb.append(" where pex.deleted = 0");
        sb.append(" and pex.classify = ?");
        sb.append(" and ISNULL(pex.before_exp)");
        sb.append(" and pex.pt_id > 100001");
        sb.append(" and pex.exploration_type = ?");
        sb.append(" and pex.stage_id = ?");

        return sb.toString();
    }


    /**
     * 返回没有配置典设的踏勘点
     *
     * @return
     */
    private String codeset() {
        StringBuilder sb = new StringBuilder();
        sb.append(" select * from p_exploration pex");
        sb.append(" INNER JOIN p_pt pt ON pt.pt_id = pex.pt_id AND pt.pt_type = 11");
        sb.append(" where pex.deleted = 0");
        sb.append(" and pex.classify = ?");
        sb.append(" and ISNULL(pex.codeset_id)");
        sb.append(" and pex.exploration_type = ?");
        sb.append(" and pex.stage_id = ?");

        return sb.toString();
    }
}
